package com.yikaiye.android.yikaiye.ui.fragment.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.BannerModel;
import com.yikaiye.android.yikaiye.model.CustomerServiceModel;
import com.yikaiye.android.yikaiye.model.HomeIconStatusModel;
import com.yikaiye.android.yikaiye.model.ProductDataModel;
import com.yikaiye.android.yikaiye.model.ProductModel;
import com.yikaiye.android.yikaiye.model.TypeModel;
import com.yikaiye.android.yikaiye.model.TypeProductModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexViewModel extends BaseViewModel {
    public MutableLiveData<List<BannerModel>> bannerData = new MutableLiveData<>();
    public MutableLiveData<List<CustomerServiceModel>> customerServiceData = new MutableLiveData<>();
    public MutableLiveData<List<TypeProductModel>> typeData = new MutableLiveData<>();
    public MutableLiveData<List<ProductModel>> productData = new MutableLiveData<>();
    public MutableLiveData<HomeIconStatusModel> homeIconStatusModelMutableLiveData = new MutableLiveData<>();

    /**
     * 轮播图
     */
    public void getBanner() {
        addSubscribe(mRetrofitHelper.getBanner(1).subscribeWith(new BaseNetCallback<List<BannerModel>>() {
            @Override
            public void onSuccess(BaseModel<List<BannerModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    bannerData.setValue(baseModel.getData());
                }
            }
        }));
    }

    /**
     * 获取用户专属客服
     */
    public void getCustomerService() {
        addSubscribe(mRetrofitHelper.getCustomerService().subscribeWith(new BaseNetCallback<List<CustomerServiceModel>>() {
            @Override
            public void onSuccess(BaseModel<List<CustomerServiceModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    customerServiceData.setValue(baseModel.getData());
                }
            }
        }));
    }

    /**
     * 产品分类
     */
    public void getTypeList() {
        Map<String, Object> map = new HashMap<>();
        map.put("page", 1);
        map.put("pageSize", 100);
        map.put("source", "app");
        addSubscribe(mRetrofitHelper.getTypeList(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<TypeModel>() {
            @Override
            public void onSuccess(BaseModel<TypeModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode() && baseModel.getData() != null) {
                    typeData.setValue(baseModel.getData().getRecords());
                }
            }
        }));
    }

    /**
     * 热门推荐产品
     */
    public void getProductList() {
        Map<String, Object> map = new HashMap<>();
        map.put("page", 1);
        map.put("pageSize", 10);
        addSubscribe(mRetrofitHelper.getProductList(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<ProductDataModel>(){

            @Override
            public void onSuccess(BaseModel<ProductDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode() && baseModel.getData() != null) {
                    productData.setValue(baseModel.getData().getRecords());
                }
            }
        }));
    }

    /**
     * 首页开票入口状态（暂时无用）
     */
    public void getHomeIconStatus() {
        addSubscribe(mRetrofitHelper.getHomeIconStatus().subscribeWith(new BaseNetCallback<HomeIconStatusModel>(){

            @Override
            public void onSuccess(BaseModel<HomeIconStatusModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode() && baseModel.getData() != null) {
                    homeIconStatusModelMutableLiveData.setValue(baseModel.getData());
                }
            }
        }));
    }
}
